package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/users")
@Slf4j
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    //用户列表查询
    @GetMapping
    public Result userList(UserDto userDto){
    log.info("查询用户列表:{}",userDto);
    PageResult<User> pageResult=userService.userList(userDto);
    return Result.success(pageResult);
}

    //用户列表批量删除
    @Log
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer>ids){
    log.info("删除用户:{}",ids);
    userService.deleteByIds(ids);
    return Result.success();
}

    //添加用户
    @Log
    @PostMapping
    public Result add(@RequestBody User user){
        log.info("添加用户:{}",user);
        userService.add(user);
        return Result.success();
    }
    //根据id查询用户
    @GetMapping("/{id}")
    public Result getByID(@PathVariable Integer id){
        log.info("根据ID查询用户:{}",id);
       User user =userService.getByID(id);
        return Result.success(user);
    }
    //修改用户
    @Log
    @PutMapping
    public Result update(@RequestBody User user){
        log.info("修改用户:{}",user);
        userService.upDate(user);
        return Result.success();
    }
    //查询所有用户
    @GetMapping("/list")
    public Result allList(User user){
        log.info("查询所有用户:{}",user);
        return userService.allList(user);
    }
    //根据角色查询用户
    @GetMapping("/role/{roleLabel}")
    public Result roleList(@PathVariable String roleLabel){
        log.info("根据角色查询用户:{}",roleLabel);
        return userService.roleList(roleLabel);
    }
    //根据部门查询用户
    @GetMapping("/dept/{deptId}")
    public Result deptList(@PathVariable Integer deptId){
        log.info("根据部门查询用户:{}",deptId);
        return userService.deptList(deptId);
    }
}
